package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.base.Constant;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.MemberLoginSmsBean;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.model.net.bean.ThirdLoginBean;
import com.cljz.makepassenger.ui.activity.LoginActivity;
import com.cljz.makepassenger.utils.L;
import com.clzj.makepassenger.bean.UserInfo;
import com.google.gson.Gson;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/1.
 */

public class LoginP extends BaseP<LoginActivity> {

    public void getSms(String phone) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("send_type", Constant.SmsType.SMS_LOGIN);
        paramsMap.put("phone", phone);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getSmsSendData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean smsSendBean) {
                        if ("0".equals(smsSendBean.getError_code())) {
                            getV().getSmsSucceed();
                        } else if ("1000".equals(smsSendBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getSmsFail(smsSendBean.getMsg());
                        }
                    }
                });

    }

    public void loginBySms(String phone, String code) {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("code", code);
        paramsMap.put("phone", phone);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getSmsLoginData(requestBody)
                .compose(XApi.<MemberLoginSmsBean>getApiTransformer())
                .compose(XApi.<MemberLoginSmsBean>getScheduler())
                .compose(getV().<MemberLoginSmsBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<MemberLoginSmsBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(MemberLoginSmsBean memberLoginSmsBean) {
                        if ("0".equals(memberLoginSmsBean.getError_code())) {
                            String token = memberLoginSmsBean.getData().getToken();
                            SharedPref.getInstance(App.getInstance()).putString(Constant.TOKEN, token);
                            //TODO信息存储到数据库
                            MemberLoginSmsBean.DataBean dataBean = memberLoginSmsBean.getData();
                            UserInfo userInfo = new UserInfo();
                            userInfo.setToken(dataBean.getToken());
                            userInfo.setBirthday(dataBean.getBirthday());
                            userInfo.setUser_logo(dataBean.getUser_logo());
                            userInfo.setUser_nickname(dataBean.getUser_nickname());
                            userInfo.setUser_phone(dataBean.getUser_phone());
                            userInfo.setUser_sex(dataBean.getUser_sex());
                            userInfo.setEid(dataBean.getEid());
                            userInfo.setAge(dataBean.getAge());
                            updateDB(userInfo);
                            getV().loginSucceed(memberLoginSmsBean.getData());
                        } else if ("1000".equals(memberLoginSmsBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().loginFail(memberLoginSmsBean.getMsg());
                        }
                    }
                });

    }

    public void loginByThird(final SHARE_MEDIA platform, final Map<String, String> data) {
        HashMap<String, String> paramsMap = new HashMap<>();
        L.e("loginByThird",data.toString());
        if (platform == SHARE_MEDIA.QQ) {
            paramsMap.put("user_from", "qq");
        } else if (platform == SHARE_MEDIA.WEIXIN) {
            paramsMap.put("user_from", "wx");
            paramsMap.put("unionid", data.get("unionid"));
        }
        paramsMap.put("p_type", "android");
        paramsMap.put("openid", data.get("openid"));
        paramsMap.put("access_token", data.get("access_token"));
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getThirdLoginData(requestBody)
                .compose(XApi.<ThirdLoginBean>getApiTransformer())
                .compose(XApi.<ThirdLoginBean>getScheduler())
                .compose(getV().<ThirdLoginBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ThirdLoginBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        L.e(error.toString());
                    }

                    @Override
                    public void onNext(ThirdLoginBean thirdLoginBean) {
                        L.e(thirdLoginBean.getError_code() + "::" + thirdLoginBean.getMsg());
                        if ("0".equals(thirdLoginBean.getError_code())) {
                            L.e(thirdLoginBean.getFlag() + "");
                            if ("0".equals(thirdLoginBean.getFlag())) {
                                SharedPref.getInstance(App.getInstance()).putString(Constant.TOKEN, thirdLoginBean.getData().getToken());
                                ThirdLoginBean.DataBean dataBean = thirdLoginBean.getData();
                                UserInfo userInfo = new UserInfo();
                                userInfo.setToken(dataBean.getToken());
                                userInfo.setBirthday(dataBean.getBirthday());
                                userInfo.setUser_logo(dataBean.getUser_logo());
                                userInfo.setUser_nickname(dataBean.getUser_nickname());
                                userInfo.setUser_phone(dataBean.getUser_phone());
                                userInfo.setUser_sex(dataBean.getUser_sex());
                                userInfo.setEid(dataBean.getEid());
                                userInfo.setAge(dataBean.getAge());
                                updateDB(userInfo);
                                getV().thirdLoginSucceed(thirdLoginBean);
                            } else {
                                String user_from = null;
                                if (platform == SHARE_MEDIA.QQ) {
                                    user_from = "qq";
                                } else if (platform == SHARE_MEDIA.WEIXIN) {
                                    user_from = "wx";
                                }
                                getV().bindPhone(user_from, data.get("openid"), data.get("unionid"));
                            }
                        } else {
                            getV().thirdLoginFail(thirdLoginBean.getMsg());
                        }
                    }
                });
    }


}
